﻿using AutoMapper;
using DereamWork.WebAPI;
using dreamwork.mobile.app.api.Models.Data;
using dreamwork.mobile.app.api.Models.ViewModel;
using dreamwork.mobile.app.api.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace dreamwork.mobile.app.api.Controllers
{
    [Route("api/[controller]")]
    public class NormalUserController : DreamWorkControllerBase
	{
        private readonly INormalUserRegisterService _normalUserRegisterService;
        private readonly INormalUserService _normalUserService;
        private readonly ITerminalUserService _terminalUserService;
        private readonly IMapper _mapper;
        public NormalUserController(
            INormalUserService normalUserService,
            INormalUserRegisterService normalUserRegisterService,
            ITerminalUserService terminalUserService,
            IMapper mapper)
        {
            _normalUserRegisterService = normalUserRegisterService;
            _normalUserService = normalUserService;
            _terminalUserService = terminalUserService;
            _mapper = mapper;
        }

        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult TestToken()
        {
            if (!string.IsNullOrEmpty(CurrentUserId))
            {
                return Success($"CurrentUserId: {CurrentUserId}");
            }
            else
            {
                return Error("No User");
            }
        }

        [HttpGet]
        [Route("[action]")]
        public async Task<IActionResult> GetRegistrationCodeAsync(string phoneNumber)
        {
            return Success(await _normalUserRegisterService.GetRegistrationCodeAsync(phoneNumber));
        }
        [HttpPost]
        [Route("[action]")]
        public async Task<IActionResult> RegisterAsync([FromBody]NormalUserRegisterViewModel user)
        {
            var result = await _normalUserRegisterService.RegisterAsync(_mapper.Map<NormalUser>(user));
            if (result.Item1)
                return Success(result.Item2);
            return Error(result.Item2);
        }

        [HttpPost]
        [Route("[action]")]
        public async Task<IActionResult> LoginAsync([FromBody]NormalUserLoginViewModel normalUser)
        {
            return Success(await _normalUserService.LoginAsync(_mapper.Map<NormalUser>(normalUser)));
        }


        [HttpGet]
        [Route("[action]")]
        public async Task<IActionResult> LogoutAsync()
        {
            return Success(await _normalUserService.LogoutAsync());
        }
        [HttpGet]
        [Route("[action]")]
        [Authorize]
        public async Task<IActionResult> GetUserProfileAsync()
        {
            if (string.IsNullOrEmpty(CurrentUserId))
            {
                return Error("请登录.");
            }
            var result = await _normalUserService.GetByIdAsync(CurrentUserId);
            if (result.IsSuccess)
            {
                return Success(_mapper.Map<NormalUserDetailViewModel>(result.Data));
            }
            return Error(result.Message);
        }
        [HttpPost]
        [Route("[action]")]
        public async Task<IActionResult> ModifyAsync([FromBody]NormalUserModifyViewModel normalUser)
        {
            var result = await _normalUserService.ModifyAsync(_mapper.Map<NormalUser>(normalUser));
            if (result.IsSuccess)
            {
                return Success(_mapper.Map<NormalUserDetailViewModel>(result.Data));
            }
            return Error(result.Message);
        }
        [HttpGet]
        [Route("[action]")]
        public async Task<IActionResult> GetTerminalListAsync()
        {
            var result = await _terminalUserService.GetListAsync();
            if (result.IsSuccess)
            {
                return Success(_mapper.Map<IEnumerable<TerminalUserDetailViewModel>>(result.Data));
            }
            return Error(result.Message);
        }
        [HttpGet]
        [Route("[action]")]
        public async Task<IActionResult> GetTerminalDetailByIdAsync(string id)
        {
            var result = await _terminalUserService.GetById(id);
            if (result.IsSuccess)
            {
                return Success(_mapper.Map<TerminalUserDetailViewModel>(result.Data));
            }
            return Error(result.Message);
        }
    }
}
